package com.iot.gateway.tsl.controller;

import com.iot.gateway.tsl.qo.PropertyQo;
import com.iot.gateway.tsl.service.TslPropertyService;
import com.iot.gateway.utils.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物模型管理 - 属性管理
 * 提供设备属性的动态增删改查 REST 接口
 *
 * @author guoxinyu
 * @create 2025/5/26 16:49
 */
@RestController
@RequestMapping("/api/device/property")
@Tag(name = "物模型管理 - 属性管理")
public class TslPropertyController {

    private final TslPropertyService tslPropertyService;

    public TslPropertyController(TslPropertyService tslPropertyService) {
        this.tslPropertyService = tslPropertyService;
    }

    @Operation(summary = "新增设备属性", description = "为指定设备添加一个属性")
    @PostMapping("/add/{deviceId}")
    public ApiResult<Void> addProperty(
            @Parameter(description = "设备 ID", required = true, example = "device123") @PathVariable String deviceId,
            @Parameter(description = "属性信息", required = true) @RequestBody PropertyQo property) {
        tslPropertyService.addProperty(deviceId, property);
        return ApiResult.success();
    }

    @Operation(summary = "更新设备属性", description = "更新指定设备的某个属性")
    @PostMapping("/update/{deviceId}")
    public ApiResult<Void> updateProperty(
            @Parameter(description = "设备 ID", required = true, example = "device123") @PathVariable String deviceId,
            @Parameter(description = "属性信息，包含要更新的 ID", required = true) @RequestBody PropertyQo property) {
        tslPropertyService.updateProperty(deviceId, property);
        return ApiResult.success();
    }

    @Operation(summary = "删除设备属性", description = "删除指定设备的某个属性")
    @GetMapping("/del/{deviceId}/{propertyId}")
    public ApiResult<Void> deleteProperty(
            @Parameter(description = "设备 ID", required = true, example = "device123") @PathVariable String deviceId,
            @Parameter(description = "属性 ID", required = true, example = "550e8400-e29b-41d4-a716-446655440000") @PathVariable String propertyId) {
        tslPropertyService.deleteProperty(deviceId, propertyId);
        return ApiResult.success();
    }

    @Operation(summary = "查询设备属性列表", description = "获取指定设备的所有属性")
    @GetMapping("/select/{deviceId}")
    public ApiResult<List<PropertyQo>> getProperties(
            @Parameter(description = "设备 ID", required = true, example = "device123") @PathVariable String deviceId) {
        List<PropertyQo> list = tslPropertyService.getProperties(deviceId);
        return ApiResult.success(list);
    }
}
